package org.example.springgobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户上线管理器
 */
@Component
public class OnlineUserManager {
    //第一个哈希表用来表示当前用户在游戏大厅的在线状态 注意线程安全
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();

    //第一个哈希表用来表示当前用户在游戏大厅的在线状态 注意线程安全
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    //gameHall
    public void enterGameHall(int userId, WebSocketSession session) {
        gameHall.put(userId, session);
    }

    public void exitGameHall(int userId) {
        gameHall.remove(userId);
    }

    public WebSocketSession getGameHall(int userId) {
        return gameHall.get(userId);
    }

    //gameRoom
    public void enterGameRoom(int userId, WebSocketSession session) {
        gameRoom.put(userId, session);
    }

    public void exitGameRoom(int userId) {
        gameRoom.remove(userId);
    }

    public WebSocketSession getGameRoom(int userId) {
        return gameRoom.get(userId);
    }
}
